package com.example.yeujian5.yuejian5.model;

import com.example.yeujian5.yuejian5.Interface.MatchFace;
import com.example.yeujian5.yuejian5.Listenner.MatchDetailListener;
import com.example.yeujian5.yuejian5.Listenner.MatchListener;
import com.example.yeujian5.yuejian5.bean.MatchBean;
import com.example.yeujian5.yuejian5.service.MatchService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by wangmengmeng on 2017/10/10.
 */

public class MatchModel implements MatchFace {

    private Retrofit retrofit;
    private MatchService matchService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址

    public MatchModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 获得比赛列表

     */
    @Override
    public void getList(String id, String name,final MatchListener listListener) {
        matchService = retrofit.create(MatchService.class);
        Call<List<MatchBean>> call =matchService.getMatchList();
        call.enqueue(new Callback<List<MatchBean>>() {
            @Override
            public void onResponse(Call<List<MatchBean>> call, Response<List<MatchBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<MatchBean>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    /**
     * 获得比赛详情
     */


    @Override
    public void getMatchById( String id,final MatchDetailListener listener) {
        matchService = retrofit.create(MatchService.class);
        Call<MatchBean> call= matchService.getMatchById(id);
        call.enqueue(new Callback<MatchBean>() {
            @Override
            public void onResponse(Call<MatchBean> call, Response<MatchBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<MatchBean> call, Throwable t) {

            }
        });
    }
}

